﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;

/// <summary>
/// Summary description for Default
/// </summary>
/// 

public class ForumInterface
{
    Panel panMain;
    Post[] posts;
    Topic[] topics;
    Thread[] threads;
    public Panel[] pan;
    public TextBox newPost;
    public TextBox newTopic;
    public TextBox firstPost;
    public Button[] linkButton;

    public Button sendButton;
    public Button createTopicButton;
    string currentTopic, currentThread;
    string interfaceType = "postView";

    public ForumInterface(Panel p)
    {
        this.panMain = p;
        
    }

    public void InsertPosts(Post[] posts)
    {
        this.posts = posts;   
    }
    public void InsertTopics(Topic[] topics)
    {
        this.topics = topics;
    }
    public void InsertThread(Thread[] threads)
    {
        this.threads = threads;
    }

    public void SetThread(string t) {currentThread = t;}
    public void SetTopic(string t) {currentTopic = t;}

    
    
    public void DrawLocationInterfaceForPosts()
    {
        Panel pan = new Panel();
        Label Thread = new Label();
        Label Topic = new Label();

        Thread.Attributes["class"] = "locationName";
        Topic.Attributes["class"] = "locationName";
        Thread.Text = currentThread ;
        Topic.Text = currentTopic;
        pan.Controls.Add(Thread);
        pan.Controls.Add(Topic);

        pan.Attributes["class"] = "locationInterface";
        pan.Height = 30;
        panMain.Controls.Add(pan);
    }
    public void DrawLocationInterfaceForTopics()
    {
        Panel pan = new Panel();
        Label Thread = new Label();

        Thread.Attributes["class"] = "postAuthor";
        Thread.Text = currentThread;
        pan.Controls.Add(Thread);

        pan.Attributes["class"] = "locationInterface";
        pan.Height = 30;
        panMain.Controls.Add(pan);
    }

    public void DrawPosts()
    {
        if (posts != null)
        {
            int count = posts.Count();
            Button[] b1 = new Button[count];
            Label[] content = new Label[count];
            Label[] author = new Label[count];
            Label[] date = new Label[count];
            pan = new Panel[count];

            for (int i = 0; i < posts.Count(); i++)
            {

                b1[i] = new Button();
                content[i] = new Label();
                author[i] = new Label();
                date[i] = new Label();
                pan[i] = new Panel();

                content[i].Attributes["class"] = "postContent";
                author[i].Attributes["class"] = "postAuthor";
                pan[i].Attributes["class"] = "post";
                b1[i].Attributes["class"] = "postButton";
                date[i].Attributes["class"] = "postDate";


                content[i].Text = posts[i].Content;
                author[i].Text = posts[i].Author;
                date[i].Text = posts[i].Date;

                b1[i].Text = "Odpowiedz";

                pan[i].Controls.Add(author[i]);
                pan[i].Controls.Add(date[i]);
                pan[i].Controls.Add(content[i]);
                pan[i].ID = i.ToString(); // --zamienic na id postu
         

                panMain.Controls.Add(pan[i]);
            }
        }
    }
    public void DrawPostAddingInterface()
    {
        newPost = new TextBox();
        sendButton = new Button();
        Panel panel1 = new Panel();



        newPost.Attributes["class"] = "newPost";
        panel1.Attributes["class"] = "send";
        sendButton.Attributes["class"] = "postButton";
        sendButton.Text = "Send";
        sendButton.ID = "sendPost";
        newPost.TextMode = TextBoxMode.MultiLine;

        panel1.Controls.Add(newPost);
        panel1.Controls.Add(sendButton);
        
        panMain.Controls.Add(panel1);
    }
   
    public void DrawTopics()
    {
        if (topics != null)
        {
            int count = topics.Count();
            HyperLink[] name = new HyperLink[count];
            Label[] author = new Label[count];
            Label[] date = new Label[count];
            linkButton = new Button[count];
            Panel[] pan = new Panel[count];


            for (int i = 0; i < topics.Count(); i++)
            {
                author[i] = new Label();
                date[i] = new Label();
                name[i] = new HyperLink();
                pan[i] = new Panel();
                linkButton[i] = new Button();

                author[i].Attributes["class"] = "postAuthor";
                pan[i].Attributes["class"] = "post";
                date[i].Attributes["class"] = "postDate";
                name[i].Attributes["class"] = "topicName";
                linkButton[i].Attributes["class"] = "linkButton";

                author[i].Text = topics[i].Author;
                date[i].Text = topics[i].Date;
                name[i].Text = topics[i].Name;
                linkButton[i].CommandArgument = topics[i].id.ToString();

                pan[i].Controls.Add(linkButton[i]);
                pan[i].Controls.Add(author[i]);
                pan[i].Controls.Add(date[i]);
                pan[i].Controls.Add(name[i]);

                pan[i].Height = 30;


                panMain.Controls.Add(pan[i]);
            }
        }
    }
    public void DrawTopicAddingInterface()
    {
        newTopic = new TextBox();
        firstPost = new TextBox();
        sendButton = new Button();
        Panel pan = new Panel();

        newTopic.Attributes["class"] = "newPost";
        firstPost.Attributes["class"] = "newPost";

        pan.Attributes["class"] = "send";
        sendButton.Attributes["class"] = "postButton";
        sendButton.Text = "Create";
        
        firstPost.TextMode = TextBoxMode.MultiLine;
        newTopic.Height = 20;
        firstPost.Height = 60;

        pan.Controls.Add(newTopic);
        pan.Controls.Add(sendButton);
        pan.Controls.Add(firstPost);
        

        panMain.Controls.Add(pan);

    }

    public void DrawThreads()
    {
        int count = threads.Count();
        Label[] name = new Label[count];
        pan = new Panel[count];
        linkButton = new Button[count];

        for (int i = 0; i < threads.Count(); i++)
        {
            name[i] = new Label();
            pan[i] = new Panel();
            linkButton[i] = new Button();

            name[i].Attributes["class"] = "threadName";
            pan[i].Attributes["class"] = "post";
            linkButton[i].Attributes["class"] = "linkButton";
            name[i].Text = threads[i].Name;
            linkButton[i].CommandArgument = threads[i].id.ToString();

            pan[i].Controls.Add(linkButton[i]);
            pan[i].Height = 30;
            pan[i].Controls.Add(name[i]);
            panMain.Controls.Add(pan[i]);
        }
    }

    public void DrawInterface()
    { 
     switch (interfaceType)
        {  
            case "threadView":
                DrawThreads();
            break;

            case "topicView":
                DrawTopics();
            break;

            case "postView":
                DrawPosts();
            break;

            case "locationView":
            DrawLocationInterfaceForPosts();
            break;

            case "topicAddingView":
            DrawTopicAddingInterface();
            break;

            case "postAddingView":
            DrawPostAddingInterface();
            break;

            default:
            break;
        }
        
    
    }

    public string GetSetInterfaceType { get { return interfaceType; } set { interfaceType = value;}}
    public string GetSetNewPostText { get{return newPost.Text;} set { newPost.Text = value;}}
    public string GetSetNewTopicText { get { return newTopic.Text; } set { newTopic.Text = value; } }
    public string GetSetFirstPostText { get { return firstPost.Text; } set { firstPost.Text = value; } }
    
}